extends FunctionCard

func _init() -> void:
	cost = 5
	card_id = "OneMore"

func _ready() -> void:
	card_name = "再来一次"
	self_destruction = false
	self_description = "结算结束后，再一次结算"
	trigger_timing = 3
	label.text = card_name
	self_descrpition_label.text = self_description
	

func cardFunction(deck,scoreMapping,selectedCardsDetail):
	showRunDetail()
	run_detail.text = "再来!!!"
	var parent = get_parent()  # 获取父节点
	if parent:
		var grandParent = parent.get_parent()  # 获取父节点的父节点（即祖父节点）
		var selectedMahjongs = grandParent.selectedMahjongs
		grandParent.gameProgress = 2 # 为了重计算 更新为2
		await grandParent.startCheckSelectedMahjongs(selectedMahjongs)

		# 找到 OneMore功能卡前面的所有功能卡，并执行重新执行gameprogress为3的功能可
		grandParent.gameProgress = 3 # 为了调用后面的功能牌 更新为3
		var index_of_onemore = -1
		for i in range(grandParent.funcCardsObjArr.size()):
			if grandParent.funcCardsObjArr[i].card_id == "OneMore":  # 找到包含 "OneMore" 的元素
				index_of_onemore = i
				break
		if index_of_onemore != -1:
			print("index_of_onemore=",index_of_onemore)
			var new_arr = grandParent.funcCardsObjArr.slice(0, index_of_onemore)
			print(new_arr)
			if(new_arr.size() > 0):
				await grandParent.runFuncCards(new_arr,selectedMahjongs)#结算类功能牌循环调用
		else:
			print("未找到 OneMore 元素")
	await get_tree().create_timer(0.5).timeout  # 等待
	hideRunDetail()
